<?php
include_once 'fun.php';
include_once 'islogin.php';
$_SESSION['isadmin']=isset($_SESSION['isadmin'])?$_SESSION['isadmin']:'';
    if ($_SESSION['isadmin']!=4&&$_SESSION['isadmin']!=-1) {
        header('Refresh:1;url=shop.php');
        die("请以用户管理员身份登录");
    }
    //var_dump($_POST);
    $Name = trim($_POST['Name']);
    $admin = isset($_POST['admin'])?$_POST['admin']:'';
    $isadmin = isset($_POST['isadmin'])?$_POST['isadmin']:'';
    $id=(int)$_POST['id'];
    $con=connect();
    
    if(empty($Name))
    {
    header('Refresh:0;url=user.php');
    die("姓名不能为空");
    }
        if($admin!='')
        {
            $sql = "update user set username='{$Name}',isadmin='{$admin}' where id={$id};";
        query($sql,$con);
        
        if($admin==0)
        {
            $res=query("select stuffid from stuff where id =$id",$con);
            $num=$res->num_rows;
            //var_dump($id);
            if($num!=0)
            {
                $sql="update stuff set Name = '{$Name}' where id=$id";
            }
            else
            {
               
                $sql = "insert into stuff (Name,id) values ('{$Name}',{$id})";
            }
            query($sql,$con);
        }
        else{
            if($isadmin=='销售人员'){
                $sql="delete from stuff where id=$id";
                query($sql,$con);
            }
        }
        }
        else
        {
            if($isadmin=='销售人员')
            $sql = "update user,stuff set user.username='{$Name}',stuff.Name='{$Name}' where user.id={$id} and stuff.id={$id};";
            else
            $sql="update user set username='{$Name}' where id={$id};";
            query($sql,$con);
        }
        
        echo '保存成功';
        
   header('Refresh:0;url=user.php');
    
    
    
    
?>